Fix/phpstan types #51
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request introduces several improvements and fixes across the codebase, focusing on type safety, code clarity, and consistency in handling model relationships and data transformations. The changes include stricter type annotations, improved handling of nullable values, and the addition of helper methods for formatting. Below is a breakdown of the most important changes:
Type Safety and Annotations
@phpstan-useannotations in multiple models to use\Illuminate\Database\Eloquent\Factories\HasFactory<self>for better type inference and consistency (app/Models/Article.php,app/Models/Category.php,app/Models/User.php, etc.). [1] [2] [3]BelongsTo,HasMany, andBelongsToManyrelationships (app/Models/Article.php,app/Models/Tag.php, etc.). [1] [2]Data Transformation Improvements
toArraymethods by explicitly checking for\DateTimeInterfacebefore callingtoISOString(app/Http/Resources/V1/Article/ArticleResource.php,app/Http/Resources/V1/Auth/UserResource.php). [1] [2]formatDateTimeinUserResourceto encapsulate the logic for formatting date-time values.Input Validation and Processing
GetCommentsControllerby explicitly checking if inputs are numeric before casting them to integers, ensuring robustness against invalid input (app/Http/Controllers/Api/V1/Article/GetCommentsController.php).Middleware Enhancements
OptionalSanctumAuthenticatemiddleware to ensure the$accessTokenis an instance ofPersonalAccessTokenbefore proceeding, enhancing runtime safety (app/Http/Middleware/OptionalSanctumAuthenticate.php).Code Cleanup and Consistency
UserResourceby streamlining the logic for iterating over roles and permissions (app/Http/Resources/V1/Auth/UserResource.php).These changes collectively improve the maintainability, safety, and clarity of the codebase.